﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace Movies
{
    public partial class FormSaveLocationOptions : Form
    {
        public FormSaveLocationOptions()
        {
            InitializeComponent();
        }

        private void isSaveAtMovie_CheckedChanged(object sender, EventArgs e)
        {
            centerLocPanel.Enabled = false;
            movieLocPanel.Enabled = true;
        }

        private void isSaveAtCustom_CheckedChanged(object sender, EventArgs e)
        {
            centerLocPanel.Enabled = true;
            movieLocPanel.Enabled = false;
        }

        private void selectFolder_Click(object sender, EventArgs e)
        {

            folderBrowserDialog.SelectedPath = HelperSettingProvider.Default().MovieDataStoreLocation;
            DialogResult result =  folderBrowserDialog.ShowDialog();
            if (result == DialogResult.OK)
            {
                saveLocation1.Text = folderBrowserDialog.SelectedPath;
                saveLocation2.Text = folderBrowserDialog.SelectedPath;
            }
        }

        private void OptionsSaveLocation_Load(object sender, EventArgs e)
        {
            if (HelperSettingProvider.Default().MovieDataStoreLocation != null)
            {
                if (!Directory.Exists(HelperSettingProvider.Default().MovieDataStoreLocation))
                {
                    Directory.CreateDirectory(HelperSettingProvider.Default().MovieDataStoreLocation);
                }
            }
            saveLocation1.Text = HelperSettingProvider.Default().MovieDataStoreLocation;
            saveLocation2.Text = HelperSettingProvider.Default().MovieDataStoreLocation;

            if (HelperSettingProvider.Default().DefaultStore.Trim().Equals("0"))
            {
                isSaveAtMovie.Checked = true;
            }
            else
            {
                isSaveAtCustom.Checked = true;
            }
        }

        private void saveLocation_KeyPress(object sender, KeyPressEventArgs e)
        {
            e.Handled = true;
        }

        private void cancelButton_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }

        private void applyChangeButton_Click(object sender, EventArgs e)
        {
            if (isSaveAtCustom.Checked)
            {
                HelperSettingProvider.Default().MovieDataStoreLocation = saveLocation1.Text;
                HelperSettingProvider.Default().DefaultStore = "1";
            }
            else
            {
                HelperSettingProvider.Default().MovieDataStoreLocation = saveLocation1.Text;
                HelperSettingProvider.Default().DefaultStore = "0";
            }
            HelperSettingProvider.Default().Save();
            this.Dispose();
        }
    }
}
